﻿using com.gyz.hao.WinCapInfoApp.BLL;
using com.gyz.hao.WinCapInfoApp.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using yeqw.FrameWork;
namespace com.gyz.hao.WinCapInfoApp
{
    public partial class FrmMain : Form
    {
        bool GroupCollect = false;//是否分组采集
        int CollectNum = 0;//当前分组采集的个数
        int CollectItem = 0;//从第几个开始采集
        public FrmMain(string[] arg)
        {
            if(arg.Length == 2)
            {
                GroupCollect = true;
                CollectItem = Convert.ToInt32(arg[0]);
                CollectNum = Convert.ToInt32(arg[1]);
            }
            InitializeComponent();
        }
        private delegate void ThreadRichMsg(string i);
        private delegate void ThreadLbMsg(string i);
        private void FrmMain_Load(object sender, EventArgs e)
        {
            AppConfigInfo.GetInstance(AppDomain.CurrentDomain.BaseDirectory + "\\XMLData\\AppConfig.xml");
            //初始化针对于软件设置的属性
            
            AppConfigInfo.AppConfig.WriteCapInfoEvent += AppConfig_WriteCapInfoEvent; 
            if(GroupCollect == true)
            {
                
                AppConfigInfo.AppConfig.GroupCollect = true;
                AppConfigInfo.AppConfig.CollectItem = CollectItem * CollectNum;//用于sql语句的LIMIT item,num中的item
                AppConfigInfo.AppConfig.CollectNum = CollectNum;//用于sql语句的LIMIT item,num中的Num
                grouptext.Text = "当前分组:" + CollectItem + "\r\n采集个数:" + CollectNum;
                btn_Start_Click(sender,e);//自动打开开始监控
            }
        }

        private void AppConfig_WriteCapInfoEvent(string msg)
        {
            try
            {
                if (AppConfigInfo.AppConfig.IsStop) return;
                //IsStop是开关 表示用户打开开始监控或者关闭开始监控
                if (this.lb_Msg.InvokeRequired)
                {//C#中禁止跨线程直接访问控件，InvokeRequired是为了解决这个问题而产生的，
                 //当一个控件的InvokeRequired属性值为真时，说明有一个创建它以外的线程想访问它。
                    ThreadRichMsg fc = new ThreadRichMsg(AppConfig_WriteCapInfoEvent);
                    this.Invoke(fc, new object[1] { msg });
                    //如果从另一个线程调用控件的方法，那么必须使用控件的一个 Invoke 方法来将调用封送到适当的线程。
                }
                else
                {
                    this.lb_Msg.Text = msg;
                }
            }
            catch (Exception ex)
            {
                AppConfigInfo.AppConfig.WriteMsg("LbMsg线程显示异常,异常原因:" + ex.Message);
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {//程序入口

            AppConfigInfo.AppConfig.StartTime = DateTime.Now;
            AppConfigInfo.AppConfig.IsStop = false;
            timer_msg.Interval = 1000;
            timer_msg.Start();//负责显示采集时间的
            CapInfoBLL bll = new CapInfoBLL();


            bll.Exec(GroupCollect);
        }
        public static String formatDuring(long mss)
        {
            long days = mss / (1000 * 60 * 60 * 24);
            long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
            long seconds = (mss % (1000 * 60)) / 1000;
            return days + " days " + hours + " hours " + minutes + " minutes "
                    + seconds + " seconds ";
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            AppConfigInfo.AppConfig.IsStop = true;
            timer_msg.Stop();
            lb_Msg.Text = "用户停止监控";
            this.lb_timer.Text = this.lb_timer.Text.Replace("正在", "停止");

        }


        private void timer_msg_Tick(object sender, EventArgs e)
        {
            if (this.lb_timer.InvokeRequired)
            {
              //
            }
            else
            {
                TimeSpan ts = DateTime.Now -AppConfigInfo.AppConfig.StartTime;
                string dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒";

                this.lb_timer.Text = "正在监控...\t用时：[{0}]".FormatExtension(dateDiff);
            }
        }
    }
}
